<?php
class ModelCatalogOption extends Model {
	public function addOption($data) {
		$this->db->query("INSERT INTO `@@option` SET type = '" . ES($data['type']) . "', sort_order = " . (int)$data['sort_order']);
		
		$option_id = $this->db->getLastId();
		
		foreach ($data['option_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO  @@option_description SET option_id = " . $option_id . ", language_id = " . (int)$language_id . ", name = '" . ES($value['name']) . "'");
		}

		if (isset($data['option_value'])) {
			foreach ($data['option_value'] as $option_value) {
				$this->db->query("INSERT INTO  @@option_value SET option_id = " . $option_id . ", image = '" . ES(html_entity_decode($option_value['image'], ENT_QUOTES, 'UTF-8')) . "', sort_order = " . (int)$option_value['sort_order']);
				
				$option_value_id = $this->db->getLastId();
				
				foreach ($option_value['option_value_description'] as $language_id => $option_value_description) {
					$this->db->query("INSERT INTO  @@option_value_description SET option_value_id = " . $option_value_id . ", language_id = " . (int)$language_id . ", option_id = " . $option_id . ", name = '" . ES($option_value_description['name']) . "'");
				}
			}
		}
	}
	
	public function editOption($option_id, $data) {
		$option_id = intval($option_id);
		
		$this->db->query("UPDATE `@@option` SET type = '" . ES($data['type']) . "', sort_order = " . (int)$data['sort_order'] . " WHERE option_id = $option_id");

		$this->db->query("DELETE FROM  @@option_description WHERE option_id = $option_id");

		foreach ($data['option_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO  @@option_description SET option_id = " . $option_id . ", language_id = " . (int)$language_id . ", name = '" . ES($value['name']) . "'");
		}
				
		$this->db->query("DELETE FROM  @@option_value WHERE option_id = $option_id");		
		$this->db->query("DELETE FROM  @@option_value_description WHERE option_id = $option_id");
		
		if (isset($data['option_value'])) {
			foreach ($data['option_value'] as $option_value) {
				if ($option_value['option_value_id']) {
					$this->db->query("INSERT INTO  @@option_value SET option_value_id = " . (int)$option_value['option_value_id'] . ", option_id = $option_id, image = '" . ES(html_entity_decode($option_value['image'], ENT_QUOTES, 'UTF-8')) . "', sort_order = " . (int)$option_value['sort_order']);
				} else {
					$this->db->query("INSERT INTO  @@option_value SET option_id = $option_id, image = '" . ES(html_entity_decode($option_value['image'], ENT_QUOTES, 'UTF-8')) . "', sort_order = " . (int)$option_value['sort_order']);
				}
				
				$option_value_id = $this->db->getLastId();
				
				foreach ($option_value['option_value_description'] as $language_id => $option_value_description) {
					$this->db->query("INSERT INTO  @@option_value_description SET option_value_id = $option_value_id, language_id = " . (int)$language_id . ", option_id = $option_id, name = '" . ES($option_value_description['name']) . "'");
				}
			}
		}
	}
	
	public function deleteOption($option_id) {
		$this->db->query("DELETE FROM `@@option` WHERE option_id = " . (int)$option_id);
		$this->db->query("DELETE FROM  @@option_description WHERE option_id = " . (int)$option_id);	
		$this->db->query("DELETE FROM  @@option_value WHERE option_id = " . (int)$option_id);
		$this->db->query("DELETE FROM  @@option_value_description WHERE option_id = " . (int)$option_id);
	}
	
	public function getOption($option_id) {
		return $this->db->queryOne("SELECT * FROM `@@option` o LEFT JOIN  @@option_description od ON (o.option_id = od.option_id) WHERE o.option_id = '" . (int)$option_id . "' AND od.language_id = " . (int)C('config_language_id'));
	}
		
	public function getOptions($filter = null) {
		$sql = "SELECT * FROM `@@option` o LEFT JOIN  @@option_description od ON (o.option_id = od.option_id)
				WHERE od.language_id = " . (int)C('config_language_id');
		if ($filter) {
			if ($filter->get('filter_name')) {
				$sql .= " AND od.name LIKE '" . ES($filter->get('filter_name')) . "%'";
			}
			$sql .= $filter->sort('od.name');
			$sql .= $filter->limit();
		}
						
		$query = $this->db->query($sql);

		return $query->rows;
	}
	
	public function getOptionDescriptions($option_id) {
		$option_data = array();
		
		$query = $this->db->query("SELECT * FROM  @@option_description WHERE option_id = " . (int)$option_id);
				
		foreach ($query->rows as $result) {
			$option_data[$result['language_id']] = array('name' => $result['name']);
		}
		
		return $option_data;
	}
	
	public function getOptionValue($option_value_id) {
		return $this->db->queryOne("SELECT * FROM  @@option_value ov LEFT JOIN  @@option_value_description ovd
				ON (ov.option_value_id = ovd.option_value_id) WHERE ov.option_value_id = " . (int)$option_value_id
				. " AND ovd.language_id = " . (int)C('config_language_id'));
	}
	
	public function getOptionValues($option_id) {
		
		$query = $this->db->query("SELECT ov.option_value_id, ov.image, ov.sort_order, ovd.name FROM  @@option_value ov LEFT JOIN  @@option_value_description ovd
				ON (ov.option_value_id = ovd.option_value_id) WHERE ov.option_id = " . (int)$option_id
				. " AND ovd.language_id = " . (int)C('config_language_id') . " ORDER BY ov.sort_order ASC");
		
		return $query->rows;
	}
	
	public function getOptionValueDescriptions($option_id) {
		$option_value_data = array();
		
		$option_value_query = $this->db->query("SELECT * FROM  @@option_value WHERE option_id = " . (int)$option_id);
				
		foreach ($option_value_query->rows as $option_value) {
			$option_value_description_data = array();
			
			$option_value_description_query = $this->db->query("SELECT * FROM  @@option_value_description
					WHERE option_value_id = " . $option_value['option_value_id']);
			
			foreach ($option_value_description_query->rows as $option_value_description) {
				$option_value_description_data[$option_value_description['language_id']] = array('name' => $option_value_description['name']);
			}
			
			$option_value_data[] = array(
				'option_value_id'          => $option_value['option_value_id'],
				'option_value_description' => $option_value_description_data,
				'image'                    => $option_value['image'],
				'sort_order'               => $option_value['sort_order']
			);
		}
		
		return $option_value_data;
	}

	public function getTotalOptions() {
      	return $this->db->queryOne("SELECT COUNT(*) AS total FROM `@@option`");
	}		
}
?>